import axios from 'axios'

axios.defaults.baseURL = 'http://localhost:3010'

axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    return Promise.reject(error);
});

export const lists = (topCate, page) => {
    return axios.get('/products/list', {
        params: {
            topCate, page
        }
    })
}
export const cateTop = () => {
    return axios.get('/products/catetopgory')
}
export const cateSub = () => {
    return axios.get('/products/catesubgory')
}

export const cate = () => {
    return axios.get('/products/category')
}
export const ls = () => {
    return axios.get('/huser/list')
}

export const cateList = (id) => {
    return axios.get('/products/cateListid', {
        params: {
            id
        }
    })
}
export const listfind = (subCategory, page,name) => {
    return axios.get('/products/listfind', {
        params: {
            subCategory,page,name
        }
    })
}
export const ddcreates = (name,description,price,userId,image,logid,status,orderAmount,Tabs,date,num,provinceName,cityName,districtName,detailAddress) => {
    return axios.post('/order/ddcreate',{
            name,description,price,userId,image,logid,status,orderAmount,Tabs,date,num,provinceName,cityName,districtName,detailAddress
        })
}

export const dzlist = (logId) => {
    return axios.get('/di/list',{
        params:{
            logId
        }
    })
}
export const deng = (phone, password) => {
    return axios.post('/huser/login', {
        phone, password
    })
}

export const di = () => {
    return axios.get('/di/list')
}

export const tdi = (addressData) => {
    return axios.post('/di/addAddress',
        addressData
    )
}

export const qding = (logid) => {
    return axios.get('/order/quan',{
        params:logid
    })
}
export const shang = () => {
    return axios.get('/order/shang')
}

// 修正路径和参数
export const updateOrderStatus = (_id, status) => {
    return axios.post(`/order/update-status`, {
        _id,
        status
    });
};


// 删除订单（修正参数传递格式）
export const deleteOrder = (_id) => {
    return axios.delete('/order/delete', {  // 路径如果确认是 /order/delete 则保持不变
        data: { _id }  // 关键修复：参数必须放在 data 中，后端才能从 req.body 获取
    });
};

export const confirmReceive = (_id) => {
  return axios.post('/order/confirm-receive', { _id });
}
export default axios